package com.bosicen.crm.tools;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bosicen.crm.dao.entity.OrderDetail;
import com.bosicen.crm.dao.entity.Product;
import com.bosicen.crm.dao.entity.ProductTable;
import com.bosicen.crm.service.CommonServiceImpl;
import com.bosicen.crm.service.sysmanage.ProductServiceImpl;

/****
 * 订单计算
 * 
 * @author Administrator
 *
 */
@Service("calculateorder")
public class CalculateOrder {
	@Autowired
	ProductServiceImpl productService;
	@Autowired
	CommonServiceImpl commonService;
	/***
	 * 计算门的尺寸
	 * @param detail
	 * @return
	 */
	public  OrderDetail Calculate_Doors(OrderDetail detail){
		try{
			
			
			String originalhigh=detail.getOriginalhigh();//高
			String originalwidth=detail.getOriginalwidth();//宽
			String originallength=detail.getOriginallength();//厚
			
			if(detail.getIshole()==1){//如果是门洞尺寸，需要扣除相应的尺寸
				if(detail.getIsthick()==0){//未扣除地砖
					switch(detail.getIsthick_false_value()){
						case 1:originalhigh=AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)-15); break;
						case 2:originalhigh=AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)-40);break;
						case 3:originalhigh=AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)-20);break;
					}
				}
				
				if(detail.getIssingle()>0){//单丁墙计算
					switch(detail.getIssingle()){
						case 1: originalwidth=AppUtil.ObjectToString(AppUtil.ObjectToInt(originalwidth)-30); break;
						case 2: originalwidth=AppUtil.ObjectToString(AppUtil.ObjectToInt(originalwidth)-60); break;
					}
				}
			}
			
			detail.setOriginalhigh(originalhigh);
			detail.setOriginalwidth(originalwidth);
			detail.setOriginallength(originallength);
			//计算门扇/ 门扇/ 线条 尺寸
		   detail=this.Calculate_Doors_size(detail);
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return detail;
	}
	/****
	 * 计算门 的  门扇/门套/ 线条 尺寸
	 * @param detail
	 * @return
	 */
	public OrderDetail Calculate_Doors_size(OrderDetail detail){
		Product product=this.productService.findProductById(detail.getProductid());
		String originalhigh=detail.getOriginalhigh();//高
		String originalwidth=detail.getOriginalwidth();//宽
		String originallength=detail.getOriginallength();//厚
		
		ProductTable pt_1=null;ProductTable pt_2=null;
		if(detail.getFacetableid()==0){//面板
			pt_1=new ProductTable();
			pt_1.setPrice(AppUtil.ObjectToFloat(detail.getFacetable_qt_price()));
		}else{
			pt_1=this.commonService.findProductTableInfo(detail.getFacetableid() );
		}
		
		if(detail.getDoorslineid()==0){//线条
			pt_2=new ProductTable();
			pt_2.setPrice(AppUtil.ObjectToFloat(detail.getDoorsline_qt_price()));
		}else{
			pt_2=this.commonService.findProductTableInfo(detail.getDoorslineid() );
		}
		
		//价格换算
		
		float price=detail.getSingle_price()*detail.getNumber();
		if(detail.getFacetableid()==0)
			price+=AppUtil.ObjectToFloat(detail.getFacetable_qt_price());
		else
			price+=pt_1.getPrice();	
		if(detail.getDoorslineid()==0)
			price+=AppUtil.ObjectToFloat(detail.getDoorsline_qt_price());
		else
			price+=pt_2.getPrice();
		
		switch(product.getFlag()){
		  case 1:   //平开门
				//门扇净尺寸
				detail.setConfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)-40));
				detail.setConfirmwidth(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalwidth)-70));
				detail.setConfirmlength(originallength);
				//门套净尺寸
				detail.setTbconfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getConfirmhigh())+15));
				detail.setTbconfirmwidth(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getConfirmwidth())+50));
				detail.setTbconfirmlength(originallength);
				
								
				//门边线长度
				detail.setDoorslinelength(String.valueOf(2200));
				if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())>2120){
					detail.setDoorslinelength(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmhigh())+80));
				}
				detail.setDoorslinenum(4);
				
				//宽度 大于 2040
				if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())>940){
					if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())<2040){
						detail.setDoorslinelength2("2200");
					}else{
						detail.setDoorslinelength2(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmwidth())+160));
					}
					detail.setDoorslinenum2(2);
				}else{//小于 940
					if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())<2120)
						detail.setDoorslinenum(detail.getDoorslinenum()+1);
					else{
						detail.setDoorslinelength2("2200");
						detail.setDoorslinenum2(1);
					}
				}
				
				
				
				
				//超长/超高的价格
				if(AppUtil.ObjectToInt(detail.getConfirmhigh())>2050)
					price+=(AppUtil.ObjectToInt(detail.getConfirmhigh())-2050)*2/10;
				
				if(AppUtil.ObjectToInt(detail.getConfirmwidth())>850)
					price+=(AppUtil.ObjectToInt(detail.getConfirmwidth())-850)*2/10;
				
				break;
				
		  case 2:  //单推拉
			   //门扇净尺寸
				detail.setConfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)-10));
				detail.setConfirmwidth(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalwidth)-20));
				detail.setConfirmlength(originallength);
			  
//				//门边线长度
//				detail.setDoorslinelength(String.valueOf(2200));
//				detail.setDoorslinewidth(String.valueOf(2200));
//				if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())>2120)
//					detail.setDoorslinelength(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmhigh())+80));
//
//				//门边线数量
//				detail.setDoorslinenum(5);
//				if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())>940){
//					detail.setDoorslinenum(6);
//				}
				
				//超长/超高的价格
				if(AppUtil.ObjectToInt(detail.getConfirmhigh())>2050)
					price+=(AppUtil.ObjectToInt(detail.getConfirmhigh())-2050)*2/10;
				
				if(AppUtil.ObjectToInt(detail.getConfirmwidth())>850)
					price+=(AppUtil.ObjectToInt(detail.getConfirmwidth())-850)*2/10;
				
			  break;
		  case 3:  //双推拉门
			//门扇净尺寸
				detail.setConfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)-95));
				int width=(AppUtil.ObjectToInt(originalwidth)-80)/2+50;
				detail.setConfirmwidth(AppUtil.ObjectToString(width)+"+"+AppUtil.ObjectToString(width));
				detail.setConfirmlength(originallength);
				//门套净尺寸
				detail.setTbconfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getConfirmhigh())+85));	
				detail.setTbconfirmwidth(AppUtil.ObjectToString(width*2-100));
				detail.setTbconfirmlength(originallength);
				
				//门边线长度
				detail.setDoorslinelength(String.valueOf(2200));
				if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())>2120){
					detail.setDoorslinelength(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmhigh())+80));
				}
				detail.setDoorslinenum(4);
				
				//宽度 大于 2040
				if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())>940){
					if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())<2040){
						detail.setDoorslinelength2("2200");
					}else{
						detail.setDoorslinelength2(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmwidth())+160));
					}
					detail.setDoorslinenum2(2);
				}else{//小于 940
						//detail.setDoorslinenum(detail.getDoorslinenum()+1);
						if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())<2120)
							detail.setDoorslinenum(detail.getDoorslinenum()+1);
						else{
							detail.setDoorslinelength2("2200");
							detail.setDoorslinenum2(1);
						}
				}
				
				
				
				
				//超长/超高的价格
				
				if(AppUtil.ObjectToInt(detail.getConfirmhigh())>2050)
					price+=(AppUtil.ObjectToInt(detail.getConfirmhigh())-2050)*2/10;
				
				if((width*2)>1700)
					price+=(width*2-1700)*2/10;
				
				break;
			  
		  case 4:  //对开门
				//门扇净尺寸
					detail.setConfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)-40));
					int width_dkm=(AppUtil.ObjectToInt(originalwidth)-80)/2+5;
					detail.setConfirmwidth(AppUtil.ObjectToString(width_dkm)+"+"+AppUtil.ObjectToString(width_dkm));
					detail.setConfirmlength(originallength);
					//门套净尺寸
					detail.setTbconfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getConfirmhigh())+15));	
					detail.setTbconfirmwidth(AppUtil.ObjectToString(width_dkm*2-10+54));
					detail.setTbconfirmlength(originallength);
					
					//门边线长度
					detail.setDoorslinelength(String.valueOf(2200));
					if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())>2120){
						detail.setDoorslinelength(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmhigh())+80));
					}
					detail.setDoorslinenum(4);
					
					//宽度 大于 2040
					if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())>940){
						if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())<2040){
							detail.setDoorslinelength2("2200");
						}else{
							detail.setDoorslinelength2(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmwidth())+160));
						}
						detail.setDoorslinenum2(2);
					}else{//小于 940
						if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())<2120)
							detail.setDoorslinenum(detail.getDoorslinenum()+1);
						else{
							detail.setDoorslinelength2("2200");
							detail.setDoorslinenum2(1);
						}
					}
					
					////超长/超高的价格
					if(AppUtil.ObjectToInt(detail.getConfirmhigh())>2050)
						price+=(AppUtil.ObjectToInt(detail.getConfirmhigh())-2050)*2/10;
					
					if((width_dkm*2)>1700)
						price+=(width_dkm*2-1700)*2/10;
					
					break;
					
		  case 5:  //子母门 
				//门扇净尺寸
					detail.setConfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)-40));
					int width_z=(AppUtil.ObjectToInt(originalwidth)-80)/3+5;
					int width_m=((AppUtil.ObjectToInt(originalwidth)-80)/3)*2+5;
					detail.setConfirmwidth(AppUtil.ObjectToString(width_z)+"+"+AppUtil.ObjectToString(width_m));
					detail.setConfirmlength(originallength);
					//门套净尺寸
					detail.setTbconfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getConfirmhigh())+15));	
					detail.setTbconfirmwidth(AppUtil.ObjectToString(width_z*3-15+54));
					detail.setTbconfirmlength(originallength);
					
					//门边线长度
					detail.setDoorslinelength(String.valueOf(2200));
					if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())>2120){
						detail.setDoorslinelength(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmhigh())+80));
					}
					detail.setDoorslinenum(4);
					
					//宽度 大于 2040
					if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())>940){
						if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())<2040){
							detail.setDoorslinelength2("2200");
						}else{
							detail.setDoorslinelength2(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmwidth())+160));
						}
						detail.setDoorslinenum2(2);
					}else{//小于 940
					
							if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())<2120)
								detail.setDoorslinenum(detail.getDoorslinenum()+1);
							else{
								detail.setDoorslinelength2("2200");
								detail.setDoorslinenum2(1);
							}
					}
					
					//价格没有算
					
					break;
		  case 7:  //四推拉
				//门扇净尺寸
					detail.setConfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)-95));
					String width_4tl=AppUtil.ObjectToString((AppUtil.ObjectToInt(originalwidth)-80)/4+50);
					detail.setConfirmwidth(width_4tl+"+"+width_4tl+"+"+width_4tl+"+"+width_4tl);
					detail.setConfirmlength(originallength);
					//门套净尺寸
					detail.setTbconfirmhigh(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getConfirmhigh())+85));	
					detail.setTbconfirmwidth(AppUtil.ObjectToString(AppUtil.ObjectToInt(width_4tl)*4-200));
					detail.setTbconfirmlength(originallength);
					
					//门边线长度
					detail.setDoorslinelength(String.valueOf(2200));
					if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())>2120){
						detail.setDoorslinelength(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmhigh())+80));
					}
					detail.setDoorslinenum(4);
					
					//宽度 大于 2040
					if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())>940){
						if(AppUtil.ObjectToInt(detail.getTbconfirmwidth())<2040){
							detail.setDoorslinelength2("2200");
						}else{
							detail.setDoorslinelength2(AppUtil.ObjectToString(AppUtil.ObjectToInt(detail.getTbconfirmwidth())+160));
						}
						detail.setDoorslinenum2(2);
					}else{//小于 940
						if(AppUtil.ObjectToInt(detail.getTbconfirmhigh())<2120)
							detail.setDoorslinenum(detail.getDoorslinenum()+1);
						else{
							detail.setDoorslinelength2("2200");
							detail.setDoorslinenum2(1);
						}
					}
					
					
					
				   //超长/超高的价格
					if(AppUtil.ObjectToInt(detail.getConfirmhigh())>2050)
						price+=(AppUtil.ObjectToInt(detail.getConfirmhigh())-2050)*2/10;
					
					if((AppUtil.ObjectToInt(width_4tl)*4)>3400)
						price+=(AppUtil.ObjectToInt(width_4tl)*4-3400)*2/10;
					break;
			  
		}
		detail.setPrice(price);
		
		return detail;
	}
	
	
	/***
	 * 计算门套的尺寸，价格
	 * @param detail
	 * @return
	 */
	public  OrderDetail Calculate_Loops(OrderDetail detail){
		try{

			Product product=this.productService.findProductById(detail.getProductid());
			String originalhigh=detail.getOriginalhigh();//高
			String originalwidth=detail.getOriginalwidth();//宽
			String originallength=detail.getOriginallength();//厚
			
			ProductTable pt_1=null;ProductTable pt_2=null;
			if(detail.getFacetableid()==0){//面板
				pt_1=new ProductTable();
				pt_1.setPrice(AppUtil.ObjectToFloat(detail.getFacetable_qt_price()));
			}else{
				pt_1=this.commonService.findProductTableInfo(detail.getFacetableid() );
			}
			
			if(detail.getDoorslineid()==0){//线条
				pt_2=new ProductTable();
				pt_2.setPrice(AppUtil.ObjectToFloat(detail.getDoorsline_qt_price()));
			}else{
				pt_2=this.commonService.findProductTableInfo(detail.getDoorslineid() );
			}
			
			//价格换算
			int total_length=AppUtil.ObjectToInt(originalhigh)*2+AppUtil.ObjectToInt(originalwidth);
			float price=(detail.getSingle_price()*total_length)/1000;
			
			if(detail.getFacetableid()==0)
				price+=AppUtil.ObjectToFloat(detail.getFacetable_qt_price());
			else
				price+=pt_1.getPrice();	
			if(detail.getDoorslineid()==0)
				price+=AppUtil.ObjectToFloat(detail.getDoorsline_qt_price());
			else
				price+=pt_2.getPrice();
			
			detail.setPrice(price);
			//计算尺寸 
			
			//门边线长度
			detail.setDoorslinelength(String.valueOf(2200));
			if(AppUtil.ObjectToInt(originalhigh)>2120){
				detail.setDoorslinelength(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalhigh)+80));
			}
			if(product.getFlag()==9){
				detail.setDoorslinenum(2);
			}else if(product.getFlag()==10){
				detail.setDoorslinenum(4);
			}
			
			//宽度 大于 2120
			if(AppUtil.ObjectToInt(originalwidth)>940){
				if(AppUtil.ObjectToInt(originalwidth)<2040){//小于 2040 ，单+1 ，双 +2 
					detail.setDoorslinelength2("2200");
					
				}else{// 大于2040  根据实际长度 单+1 双+2
					detail.setDoorslinelength2(AppUtil.ObjectToString(AppUtil.ObjectToInt(originalwidth)+160));
				}
				if(product.getFlag()==9){
					detail.setDoorslinenum2(1);
				}else if(product.getFlag()==10){
					detail.setDoorslinenum2(2);
				}
				
				
			}else{//小于 940
					
				   if(AppUtil.ObjectToInt(originalhigh)>2120)
						detail.setDoorslinenum(detail.getDoorslinenum()+1);
					else{
						detail.setDoorslinelength2("2200");
						detail.setDoorslinenum2(1);
					}
			}
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return detail;
	}
	
}
